package com.zangchat.service.impl;

import com.common.core.domain.AjaxResult;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.auth.COSSigner;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.COSObjectInputStream;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.utils.VersionInfoUtils;
import com.zangchat.service.CommonFileService;
import com.zangchat.utils.COSClientUtil;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommonFileServiceImpl implements CommonFileService {


    /**
     * 上传文件
     * @param files
     * @return
     */
    @Override
    public AjaxResult putFiles(MultipartFile[] files,String type) {
        try{
            String resultPath="";
            for (MultipartFile file:files) {
                //上传文件工具
                String filePath = COSClientUtil.uploadFile(file,type);
                if (resultPath.equals("")){
                    resultPath=filePath;
                }else {
                    resultPath=resultPath+","+filePath;
                }
            }
            AjaxResult ajax = AjaxResult.success("上传成功");
            ajax.put("url",resultPath);
            return ajax;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("上传失败");
        }

    }

}
